import urllib

from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db.models import permalink
from django.utils.encoding import smart_str
# Create your models here.
       
class Category(models.Model):

    url_pic = models.ImageField(upload_to='category/%Y/%m/%d')
    title = models.CharField(max_length=250) # , null=True
    def __unicode__(self):
        return self.title
         
class Regions(models.Model):
  
    title = models.CharField(max_length=250)
    id_parent = models.IntegerField(null=True,default=-1)
    def __unicode__(self):
        return self.title
        
class Announcements(models.Model):
    region = models.ForeignKey(Regions,null=True)
    category = models.ForeignKey(Category)
    url_pic = models.ImageField(upload_to='announcements/%Y/%m/%d')
    
    title = models.CharField(max_length=250)
    short_description = models.CharField(max_length=250, null=True)
    description = models.TextField(null=True)
    a_date = models.DateField()
    popularity = models.IntegerField(default=0)
    
    user = models.OneToOneField(User, null=True)
    
    def __unicode__(self):
        return self.title
        
class News(models.Model):

    url_pic = models.ImageField(upload_to='news/%Y/%m/%d')
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=250)
    short_description = models.CharField(max_length=250, null=True)
    description = models.TextField(null=True)
    a_date = models.DateField()
    popularity = models.IntegerField(default=0)
    
    user = models.OneToOneField(User, null=True)
    
    def __unicode__(self):
        return self.title

class Section(models.Model): # category of goods
    region = models.ForeignKey(Regions)
    title = models.CharField(max_length=250) # , null=True
    short_description = models.CharField(max_length=250, null=True)
    url_pic = models.ImageField(upload_to='section/%Y/%m/%d')
    def __unicode__(self):
        return self.title
        
class Goods(models.Model):

    region = models.ForeignKey(Regions)
    url_pic = models.ImageField(upload_to='goods/%Y/%m/%d')
    section = models.ForeignKey(Section)
    title = models.CharField(max_length=250) # , null=True
    short_description = models.CharField(max_length=250, null=True)
    description = models.TextField(null=True)
    a_date = models.DateField()
    popularity = models.IntegerField(default=0)
    
    user = models.OneToOneField(User, null=True)
    iLID = models.CharField(max_length=250)
    iUID = models.CharField(max_length=250)
    
    def __unicode__(self):
        return self.title
        
class Partners(models.Model):
    user = models.OneToOneField(User)
    url_pic = models.ImageField(upload_to='partners_pic/%Y/%m/%d')
    url_to = models.ImageField(upload_to='partners_to/%Y/%m/%d')
    title = models.CharField(max_length=250) # , null=True
    description = models.TextField(null=True)

    
    def __unicode__(self):
        return self.title

class Albums(models.Model):
    url_pic = models.ImageField(upload_to='albums/%Y/%m/%d')
    title = models.CharField(max_length=250)
    description = models.TextField(null=True)
    
    user = models.OneToOneField(User, null=True)
    
    def __unicode__(self):
        return self.title
    
class Photos(models.Model):
    album = models.ForeignKey(Albums)
    url_pic = models.ImageField(upload_to='photos/%Y/%m/%d')
    title = models.CharField(max_length=250)
    description = models.TextField(null=True)
    a_date = models.DateField()
    popularity = models.IntegerField(null=True)
    
    user = models.OneToOneField(User, null=True)
    
    def __unicode__(self):
        return self.title

class BillingLog(models.Model):
    a_date = models.DateField()
    reason = models.CharField(max_length=250, null=True)
    value = models.CharField(max_length=250, null=True)
    
    user = models.OneToOneField(User, null=True)

    
class BillingX(models.Model):
    a_date = models.DateField()
    start_date = models.DateField()
    end_date = models.DateField()
    
    to_pay = models.FloatField(default=0)
    payed = models.FloatField(default=0)
    different = models.FloatField(default=0)
    debit = models.FloatField(default=0)
    
    reason = models.CharField(max_length=250, null=True)
    
    user = models.OneToOneField(User, null=True)

class UserContact(models.Model):
    user = models.OneToOneField(User)
    contacts = models.TextField(null=True)
    about = models.TextField(null=True)
    url_map = models.ImageField(upload_to='userContact_map/%Y/%m/%d')
    url_priceList = models.ImageField(upload_to='userContact_price/%Y/%m/%d')

    
    def __unicode__(self):
        return self.title
   
class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User, null=True)
    is_new = models.BooleanField(default=True)
    # Other fields here
    is_payed = models.BooleanField(default=False)
    favorite_animal = models.CharField(max_length=20, default="Dragons.")
    
    def get_is_new_url(self):       
        return "/basement/show/to_activate/%s/" % urllib.quote(smart_str(self.user.id))

